#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include "MoveSeg.h"
#include "SheepSeg.h"

namespace SheepDefence
{
	MoveSeg::MoveSeg()
	{
	}

	MoveSeg::~MoveSeg()
	{
		delete x;
	}

	MoveSeg::MoveSeg(SypX::Font* f, TutorialScene* tut) : stamDone(false), tutorial(tut), Level()
	{
		font = f;

		xMesh = new SypX::Mesh();
		xMesh->initAsQuad(50.0f, 50.0f);

		x = new SypX::MeshInstance();
		x->useMesh(xMesh);
		x->setPosition(SypX::Vec3(160.0f, 360.0f, 0.1f));
		x->useTexture(SypX::Texture::loadFromFile("Textures/x.png"));

		clockMesh = new SypX::Mesh();
		clockMesh->initAsQuad(20.0f, 20.0f);

		clock = new SypX::MeshInstance();
		clock->useMesh(clockMesh);
		clock->setPosition(SypX::Vec3(260.0f, 440.0f, 0.9f));		
		clock->useTexture(SypX::Texture::loadFromFile("Textures/clock.png"));

		backgroundMesh = new SypX::Mesh();
		backgroundMesh->initAsQuad(320, 480);

		background = new SypX::MeshInstance();
		background->useMesh(backgroundMesh);
		background->setPosition(SypX::Vec3(160.0f, 240.0f, 0));		
		SypX::ErrorLogger::getLogger() << "Loading background: TUTORIAL\n";
		background->useTexture(SypX::Texture::loadFromFile("Textures/example.png"));

		SypX::ErrorLogger::getLogger() << "Brief text: " << brief << "\n";
		briefBox =  new SypX::TextBox(font, 250.0f, 250.0f, 10);
		briefBox->setBgColor(SypX::Vec4(0, 0, 0, 1.0f));
		briefBox->setBorderColor(SypX::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
		briefBox->setText("The first thing to learn about Protectors of Sheep would be to move your canine hero. Go ahead and attempt to move to the spot marked by the cross simply by touching it!");
		briefBox->getMeshInstance()->setPosition(SypX::Vec3(160.0f, 240.0f, 1.1f));

		Actor* currActor = Actor::loadActor("Actors/dog.act");
		actors.push_back(currActor);

		Instance* currInstance;
		currInstance = new DogInstance(static_cast<Dog*>(currActor), this, 160.0f, 100.0f, 6);
		dog = static_cast<DogInstance*>(currInstance);
		instances.push_back(currInstance);

		currActor = Actor::loadActor("Actors/fence.act");
		actors.push_back(currActor);

		currInstance = new ObstacleInstance(static_cast<Obstacle*>(currActor), this, 160.0f, 240.0f, 0);
		obstacles.push_back(static_cast<ObstacleInstance*>(currInstance));
		instances.push_back(currInstance);

		environment = new Graph(6, this);

		environment->nodes[0]->init(90.0f, 210.0f, environment);
		environment->nodes[1]->init(90.0f, 270.0f, environment);
		environment->nodes[2]->init(160.0f, 320.0f, environment);
		environment->nodes[3]->init(230.0f, 270.0f, environment);
		environment->nodes[4]->init(230.0f, 210.0f, environment);
		environment->nodes[5]->init(160.0f, 120.0f, environment);

		environment->nodes[0]->addNeighbour(environment->nodes[5]);
		environment->nodes[0]->addNeighbour(environment->nodes[1]);
		environment->nodes[1]->addNeighbour(environment->nodes[0]);
		environment->nodes[1]->addNeighbour(environment->nodes[2]);
		environment->nodes[2]->addNeighbour(environment->nodes[1]);
		environment->nodes[2]->addNeighbour(environment->nodes[3]);
		environment->nodes[3]->addNeighbour(environment->nodes[2]);
		environment->nodes[3]->addNeighbour(environment->nodes[4]);
		environment->nodes[4]->addNeighbour(environment->nodes[3]);
		environment->nodes[4]->addNeighbour(environment->nodes[5]);
		environment->nodes[5]->addNeighbour(environment->nodes[4]);
		environment->nodes[5]->addNeighbour(environment->nodes[0]);

		environment->update();

		changeState(BRIEFING);
	}

	void MoveSeg::advance(float time)
	{
		Level::advance(time);
		
		if (dog->stamina <= 150 && !stamDone)
		{
			changeState(BRIEFING);
			briefBox->setText("Notice your stamina bar at the right side of the screen? When it is depleted, your canine hero will only be able to trot slowly. Stamina can be regenerated by simply standing still.");
			stamDone = true;
		}

		if (dog->x > 150.0f && dog->x < 170.0f && dog->y > 350.0f && dog->y < 370.0f)
			tutorial->level = new SheepSeg(font, tutorial, xMesh, backgroundMesh, clockMesh, x, background, clock, briefBox, actors, instances, dog, obstacles, environment);
	}

	void MoveSeg::draw()
	{
		Level::draw();
		SypX::Engine::getSingleton()->getRenderer()->drawMesh(x);
	}
}